<?php
class DespositsController extends AppController {

	var $name = 'Desposits';

	function index() {
              $this->set('title_for_layout', 'Configuracion - Depósitos');
		$this->Desposit->recursive = 0;
		$this->set('desposits', $this->paginate());
                $this->loadModel('Provinces');
                $provinces = $this->Provinces->find('list',array('fields'=>'descripcion'));
                //$this->log($provinces);
                $this->set('provinces',$provinces);
	}

	function view($id = null) {
              $this->set('title_for_layout', 'Configuracion - Depósitos');
		if (!$id) {
			$this->Session->setFlash(__('Invalid desposit', true));
			$this->redirect(array('action' => 'index'));
		}
		$this->set('desposit', $this->Desposit->read(null, $id));
	}

	function add() {
              $this->set('title_for_layout', 'Configuracion - Depósitos');
		if (!empty($this->data)) {
			$this->Desposit->create();
			if ($this->Desposit->save($this->data)) {
				$this->Session->setFlash(__('El deposito fue Guardado', true));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('El deposito no su pudo guardar. Pruebe otra vez', true));
			}
		}
                $this->loadModel('Provinces');
		$locations = $this->Desposit->Locations->find('list',array('fields'=>'descripcion'));
                $provinces = $this->Provinces->find('list',array('fields'=>'descripcion'));
                $this->log($provinces);
		$this->set(compact('locations','provinces'));
	}

	function edit($id = null) {
              $this->set('title_for_layout', 'Configuracion - Depósitos');
		if (!$id && empty($this->data)) {
			$this->Session->setFlash(__('Deposito Invalido', true));
			$this->redirect(array('action' => 'index'));
		}
		if (!empty($this->data)) {
			if ($this->Desposit->save($this->data)) {
				$this->Session->setFlash(__('El deposito fue Guardado', true));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('El deposito no su pudo guardar. Pruebe otra vez', true));
			}
		}
		if (empty($this->data)) {
			$this->data = $this->Desposit->read(null, $id);
                        $idprov=$this->data['Locations']['provinces_id'];
                        $this->set('idprov',$idprov);
		}
                $this->loadModel('Provinces');
		$locations = $this->Desposit->Locations->find('list',array('fields'=>array('id','descripcion'),));
                $provinces = $this->Provinces->find('list',array('fields'=>'descripcion'));
               
		$this->set(compact('locations','provinces'));
	}

	function delete($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Deposito Invalido', true));
			$this->redirect(array('action'=>'index'));
		}
		if ($this->Desposit->delete($id)) {
			$this->Session->setFlash(__('Deposito borrado', true));
			$this->redirect(array('action'=>'index'));
		}
		$this->Session->setFlash(__('El Deposito no fue borrado', true));
		$this->redirect(array('action' => 'index'));
	}
}
